package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.annotations.Log;
import com.atguigu.spzx.manager.mapper.*;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.manager.service.ProductUnitService;
import com.atguigu.spzx.model.pojo.product.Brand;
import com.atguigu.spzx.model.pojo.product.Category;
import com.atguigu.spzx.model.pojo.product.Product;
import com.atguigu.spzx.model.pojo.product.ProductSku;
import com.atguigu.spzx.model.request.product.ProductDetails;
import com.atguigu.spzx.model.request.product.ProductQueryRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @description 针对表【product(商品)】的数据库操作Service实现
 * @createDate 2025-01-09 16:03:49
 */
@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Autowired
    private ProductUnitMapper productUnitMapper;

    @Override
    public Page<Product> findPage(Page<Product> page, ProductQueryRequest req) {
        Long brandId = req.getBrandId();
        Long category1Id = req.getCategory1Id();
        Long category2Id = req.getCategory2Id();
        Long category3Id = req.getCategory3Id();
        Page<Product> productPage = productMapper.findPage(page, brandId, category1Id, category2Id, category3Id);
        List<Product> records = productPage.getRecords();
        for (Product product : records) {
            // 品牌名
            if (product.getBrandId() != null) {
                Brand brand = brandMapper.selectById(product.getBrandId());
                product.setBrandName(brand.getName());
            }
            // 一级分类
            if (product.getCategory1Id() != null) {
                Category category = categoryMapper.selectById(product.getCategory1Id());
                product.setCategoryName1(category.getName());
            }
            // 二级分类
            if (product.getCategory2Id() != null) {
                Category category = categoryMapper.selectById(product.getCategory2Id());
                product.setCategoryName2(category.getName());
            }
            // 三级分类
            if (product.getCategory3Id() != null) {
                Category category = categoryMapper.selectById(product.getCategory3Id());
                product.setCategoryName3(category.getName());
            }
        }
        //TODO 2025/1/9 get是否是引用类型
//        productPage.setRecords(records);
        return productPage;
    }

    @Override
    @Log(title = "商品模块", businessType = 1)
    public void saveProduct(Product product) {
        // 设置商品分类名称，单元名称, 保存表product
        if (product.getCategory1Id() != null) {
            product.setCategoryName1(categoryMapper.selectById(product.getCategory1Id()).getName());
        }
        if (product.getCategory2Id() != null) {
            product.setCategoryName2(categoryMapper.selectById(product.getCategory2Id()).getName());
        }
        if (product.getCategory3Id() != null) {
            product.setCategoryName3(categoryMapper.selectById(product.getCategory3Id()).getName());
        }
        product.setAuditMessage("未审核");
        productMapper.insert(product);
        // 保存表 productSku
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            // productSku 的code，productID, skuName
            productSku.setSkuName(product.getName());
            productSku.setProductId(product.getId());
            productSkuMapper.insert(productSku);
        }
        // 保存表productDetails
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
//        int a = 1 / 0;
        productDetailsMapper.insert(productDetails);
    }

    @Override
    public Product getProductById(Long id) {
        Product product = productMapper.selectById(id);
        List<ProductSku> productSkuList = productSkuMapper.selectList(new QueryWrapper<ProductSku>().eq("product_id", id));
        product.setProductSkuList(productSkuList);
        ProductDetails productDetails = productDetailsMapper.selectOne(new QueryWrapper<ProductDetails>().eq("product_id", id));
        if (productDetails != null) {
            product.setDetailsImageUrls(productDetails.getImageUrls());
        }
        return product;
    }

    /*
     * @description: 修改商品
     **/
    @Override
    public void updateProduct(Product product) {
        // 修改商品表
        productMapper.updateById(product);
        // 修改sku，先把原来的sku都删除
        productSkuMapper.delete(new QueryWrapper<ProductSku>().eq("product_id",product.getId()));
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            // productSku 的code，productID, skuName
            productSku.setSkuName(product.getName());
            productSku.setProductId(product.getId());
            // 将原来的sku都删除 让后将新的sku添加
            productSkuMapper.insert(productSku);
        }
        // 修改details(先删后加)
        productDetailsMapper.delete(new QueryWrapper<ProductDetails>().eq("product_id", product.getId()));
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insert(productDetails);
    }

    @Override
    public void deleteProductById(Long id) {
        //1、删除product中的数据
        productMapper.deleteById(id);
        //2、删除product_sku中的数据
        productSkuMapper.delete(new QueryWrapper<ProductSku>().eq("product_id", id));
        //3、删除product_detail中的数据
        productDetailsMapper.delete(new QueryWrapper<ProductDetails>().eq("product_id", id));

    }

    @Override
    public void takeonProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(1);
        productMapper.updateById(product);
    }

    @Override
    public void takeoffProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(-1);
        productMapper.updateById(product);
    }

    @Override
    public void auditProduct(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        // 审核通过
        if (auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审核通过");
        } else if (auditStatus == -1) {
            product.setAuditStatus(-1);
            product.setAuditMessage("审核不通过");
        }
        productMapper.updateById(product);
    }
}




